package com.ficus.road.maintain.third.zh.roadgps;

import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ficus.road.maintain.core.model.Coordinate;
import com.ficus.road.maintain.core.model.RoadInfo;
import com.ficus.road.maintain.core.model.RoadTypeEnum;
import com.ficus.road.maintain.core.model.zh.roadgps.UnitGpsInfoZH;
import com.ficus.road.maintain.dao.RoadInfoDao;
import com.ficus.road.maintain.third.zh.BaseZH;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 更新城市道路
 *
 * @author xiawen yang
 * @date 2022/3/30 上午11:30
 */
@Component
@Slf4j
public class ZHCityAndDirectRoad extends BaseZH {

    @Autowired
    private RoadInfoDao roadInfoDao;

    private ObjectMapper mapper;

    @PostConstruct
    void init() {
        arg0 = "ytkjGetDyCenterPointer";
        mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    }

    public synchronized void refreshCityRoadInfo() {
        init();
        arg1 = "{\"source\":\"gls\"}";
        errMsg = "调用中海接口获取城市道路异常!";
        successMsg = "调用中海接口获取城市道路成功!";
        String cityRoadStr = getZHData();
        if (ObjectUtils.isEmpty(cityRoadStr)) {
            return;
        }
        List<UnitGpsInfoZH> unitGpsInfoZHList = JSONArray.parseArray(cityRoadStr, UnitGpsInfoZH.class);
        refreshUnitGpsInfo(unitGpsInfoZHList, RoadTypeEnum.HIGHWAY);
    }

    public synchronized void refreshDirectRoadInfo() {
        init();
        arg1 = "{\"source\":\"cgs\"}";
        errMsg = "调用中海接口获取直管公路异常!";
        successMsg = "调用中海接口获取直管公路成功!";
        String directRoadStr = getZHData();
        if (ObjectUtils.isEmpty(directRoadStr)) {
            return;
        }
        List<UnitGpsInfoZH> unitGpsInfoZHList = JSONArray.parseArray(directRoadStr, UnitGpsInfoZH.class);
        refreshUnitGpsInfo(unitGpsInfoZHList, RoadTypeEnum.DIRECT);
    }


    public void refreshUnitGpsInfo(List<UnitGpsInfoZH> unitGpsInfoZHList, RoadTypeEnum roadType) {
        Date date = new Date();
        Map<String, RoadInfo> roadInfoMap = roadInfoDao.findAll().stream().collect(Collectors.toMap(RoadInfo::getId, item -> item));
        unitGpsInfoZHList.forEach(zh -> {
            String id = zh.getSectionId() + "_" + zh.getRoadId() + "_" + zh.getStartlocation() + "_" + zh.getEndlocation();
            RoadInfo fetch = roadInfoMap.get(id);
            if (ObjectUtils.isEmpty(fetch)) {
                fetch = new RoadInfo();
                fetch.setId(id);
                fetch.setUnitId(zh.getSectionId());
                fetch.setSecNum(zh.getRoadId());
                fetch.setStartStake(zh.getStartlocation() + "");
                fetch.setEndStake(zh.getEndlocation() + "");
            }
            List<Coordinate> coordinates = new ArrayList<>();
            String wgs84 = zh.getGeojsonWgs84();
            if (ObjectUtils.isEmpty(wgs84)) {
                log.warn("roadId: {}, unitId: {}, 数据异常, wgs84为空", zh.getRoadId(), zh.getSectionId());
                return;
            }
            try {
                coordinates = mapper.readValue(wgs84, new TypeReference<List<Coordinate>>() {
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
            fetch.setRoadName(zh.getRoadName());
            fetch.setCompanyId(zh.getCompanyId());
            fetch.setCoordinates(coordinates);
            fetch.setRoadType(roadType);
            fetch.setModificationTime(date);
            roadInfoDao.saveOrUpdate(fetch);
        });
    }
}
